class_name TitleLevel
extends Node2D

@onready var camera_2d: SimpleCamera = $Camera2D

@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var ui_black_fade: UIBlackFade = $CanvasLayer/UIBlackFade

@onready var ui_title: UITitle = $UITitle
var is_title_show_finish: bool = false

@onready var ui_about: UIAbout = $CanvasLayer/UIAbout

@export var init_state: String = "Title"
@onready var states: StateMachine = $States

var window_mode_change_cooldown_time = 1.0
var window_mode_change_cooldown_timer: float = 0


func _ready() -> void:
	ui_title.level = self
	
	if init_state:
		states.transition(init_state)


func process_enter_option():
	if not is_title_show_finish: return
	
	if Input.is_action_just_pressed("use"):
		AudioManager.play_sound("sfx_menubuttonclick")
		is_title_show_finish = false
		await ui_title.hide_title().animation_finished
		states.transition("Options")
		

func get_mouse_world_position():
	return screen_2_world_pos(get_viewport().get_mouse_position()) 


func screen_2_world_pos(screen_pos: Vector2):
	var camera_left_top_pos = camera_2d.global_position
	return camera_left_top_pos + screen_pos
		
		
func on_start_new_game():
	await ui_title.hide_options().animation_finished
	await ui_black_fade.fade_in(1.0).finished
	GameManager.start_new_game()
	

func on_continue_game():
	await ui_title.hide_options().animation_finished
	await ui_black_fade.fade_in(1.0).finished
	GameManager.continue_game()
	

func on_window_mode_setting():
	await ui_title.hide_options().animation_finished
	states.transition("WindowOptions")
	

func on_about_click():
	await ui_title.hide_options().animation_finished
	states.transition("About")
	

func process_exit_window_options():
	if Input.is_action_just_pressed("exit"):
		AudioManager.play_sound("sfx_menubuttonclick")
		await ui_title.hide_window_options().animation_finished
		states.transition("Options")


func on_borderless_full_screen_button_click():
	#if window_mode_change_cooldown_timer > 0: return
	#
	#window_mode_change_cooldown_timer = window_mode_change_cooldown_time
	GlobalUtils.set_window_mode_fullscreen_borderless()
		

func on_full_screen_button_click():
	#if window_mode_change_cooldown_timer > 0: return
	#
	#window_mode_change_cooldown_timer = window_mode_change_cooldown_time
	GlobalUtils.set_window_mode_fullscreen()
	

func on_windowed_button_click():
	#if window_mode_change_cooldown_timer > 0: return
	#
	#window_mode_change_cooldown_timer = window_mode_change_cooldown_time
	GlobalUtils.set_window_mode_windowed()
	

func process_window_mode_cooldown_timer(delta: float):
	window_mode_change_cooldown_timer -= delta
	if window_mode_change_cooldown_timer <= 0:
		window_mode_change_cooldown_timer = 0
		

func process_exit_about():
	if Input.is_action_just_pressed("exit"):
		AudioManager.play_sound("sfx_paper_page_flip")
		ui_about.animation_player.play("hide")
		await ui_about.animation_player.animation_finished
		states.transition("Options")
		

func _exit_tree() -> void:
	AudioManager.stop_all_music()		
